<?php


namespace App\Http\Controllers\Admin\Mall;


use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
//商品稀有度
class ProductRarityTagController extends BaseController
{
    /**
     * @api {post} /mall/product-rarity-tag 新增稀有度标签
     *
     * @apiGroup 个性商城-稀有度标签
     *
     * @apiParam {String} rarity_tag_name 标签名称，必须
     * @apiParam {Int} status 状态：1：显示；0：隐藏，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[],"message":""}
     */
    public function add(Request $request)
    {
        $param = $request->only('rarity_tag_name', 'status');
        $v = app('validator')->make($param, [
            'rarity_tag_name' => 'required|string|max:10',
            'status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_mall_product_rarity_tag')->addOrEditProductRarityTag($param);
        return Response::sendData([]);
    }

    /**
     * @api {put} /mall/product-rarity-tag 编辑稀有度标签
     *
     * @apiGroup 个性商城-稀有度标签
     *
     * @apiParam {Int} id 标签id，必须
     * @apiParam {String} rarity_tag_name 标签名称，必须
     * @apiParam {Int} status 状态：1：显示；0：隐藏，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[],"message":""}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'rarity_tag_name', 'status');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:1',
            'rarity_tag_name' => 'required|string|max:10',
            'status' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        app('repo_mall_product_rarity_tag')->addOrEditProductRarityTag($param);
        return Response::sendData([]);
    }

    /**
     * @api {delete} /mall/product-rarity-tag 删除稀有度标签
     *
     * @apiGroup 个性商城-稀有度标签
     *
     * @apiParam {Int} id 标签id，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[],"message":""}
     */
    public function del(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $tag = app('model_product_rarity_tag')->select('id')->where('id', $param['id'])->first();
        if ($tag) {

            $product = app('model_mall_products')->select('id')->where('rarity_tag_id', $param['id'])->first();
            if ($product) throw new BasicException(0, '还有商品关联了该标签，无法删除');
            //删除
            $tag->delete();
            return Response::sendData([]);
        }
        throw new BasicException(0, '标签id错误');
    }

    /**
     * @api {get} /mall/product-rarity-tag/all 稀有度标签列表
     *
     * @apiGroup 个性商城-稀有度标签
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "id": 1, 序号
    "name": "高级", 标签名称
    "status": 1 状态：1：显示；0：隐藏
    }
    ],
    "message": ""
    }
     */
    public function all()
    {
        $list = app('model_product_rarity_tag')->select('id', 'name', 'status')->get();
        return Response::sendData($list);
    }
}